home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group94c.txt / 000041_nowlin@iwtqg.att.com _Wed Dec 28 10:55:00 1994.msg < prev    next >
Internet Message Format  |  1995-02-09  |  3KB

  1. Received: from optima.CS.Arizona.EDU by cheltenham.CS.Arizona.EDU; Wed, 28 Dec 1994 09:57:10 MST
  2. Received: from gw1.att.com by optima.CS.Arizona.EDU (5.65c/15) via SMTP
  3.     id AA17049; Wed, 28 Dec 1994 09:57:08 MST
  4. Received: from iwtqg.UUCP by ig1.att.att.com id AA08398; Wed, 28 Dec 94 11:54:57 EST
  5. Message-Id: <9412281654.AA08398@ig1.att.att.com>
  6. From: Jerry Nowlin <nowlin@iwtqg.att.com>
  7. To: icon-group@cs.arizona.edu (Icon News Group)
  8. Subject: Re: Truth-Table generator
  9. Original-To: att!cs.arizona.edu!icon-group (Icon News Group)
  10. Date: Wed, 28 Dec 94 10:55:00 CST
  11. Original-From: Jerry Nowlin <nowlin@iwtqg>
  12. X-Mailer: ELM [version 2.3 PL11]
  13.  
  14. > I had a need to write a program to output truth tables for boolean
  15. > expressions. I used the programmming language J (a sister of APL). The
  16. > 'program' was about two lines long.  I wrote a program for the same task
  17. > in Icon. I include it below.  This Icon solution is ugly (because of the
  18. > programmer, not the language..), but works. I would be happy to hear
  19. > comments. Any idea for improvemnet is welcome.
  20. >
  21. > I got alot of help on comp.lang.apl when writing the J version - so don't
  22. > let me down! I think I will write a short note comparing the solutions.
  23. > My idea is to show the way different languages shape your thought. Both J
  24. > and Icon are rather special in that they give the programmer tools not
  25. > found in other languages. Does this interest anyone?
  26. >
  27. > Any way, here goes:
  28.  
  29. It's Christmas break (unless you have a real job) and I figured why not
  30. try this.  I worked with what you had.  There were a few little things I
  31. cleaned up but the major flaw was not in the Icon.  This program didn't
  32. work with n larger than 2.  That's not an Icon problem but what the heck.
  33. It's been a while since I worried about boolean anything but I took a
  34. shot.  I also made it possible to pass in a larger n to verify the
  35. changes worked.  I didn't include the original program so dig up the
  36. earlier mail for comparison.
  37.  
  38. Jerry Nowlin
  39.  
  40. # Truth-Table generator Ver -1.0
  41. # 12.1994
  42. #
  43. procedure main(args)
  44.         local n, t
  45.  
  46.         n := get(args) | stop("I need a 'n'")
  47.  
  48.         # every write(Outl(t:=Truth_Table(n)),"--> ",Expr(t))
  49.         # shows many expr to many truth-table lines
  50.         every t:=Truth_Table(n)           &
  51.               write()                     &
  52.               writes(Outl(t))             &
  53.               every writes(" | ",Expr(t))
  54.         write()
  55.  
  56. end
  57.  
  58. procedure Truth_Table(n)
  59. #
  60. # generates lines (as lists) of the truth table for n variables
  61. # list concatenation can't be that efficient but it's simpler
  62. #
  63.         if n = 0 then return []
  64.         suspend [0|1] ||| Truth_Table(n-1)
  65. end
  66.  
  67. procedure Expr(vec)
  68. # evaluates a list of bits (the logical expr)
  69. # when multiple expr are suspended a mechanism in main prints them side by
  70. # side
  71. #
  72. # you need copies of the list since it's eaten at it's booled
  73. #
  74.         suspend Lior(copy(vec)) | Lixor(copy(vec))
  75. end
  76.  
  77. procedure Lior(vec)
  78.         if *vec = 1 then return get(vec)
  79.         suspend ior(get(vec),Lior(vec))
  80. end
  81.  
  82. procedure Lixor(vec)
  83.         if *vec = 1 then return get(vec)
  84.         suspend ixor(get(vec),Lixor(vec))
  85. end
  86.  
  87. procedure Outl(vec)
  88. #
  89. # prints a list
  90. #
  91.         local s
  92.         s := ""
  93.         every s ||:= !vec || " "
  94.         return s
  95. end